<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\base;

use Yii;
use yii\web\Response;
use builder\helper\App;
use builder\helper\Http as HttpHelper;

/**
 * HTTP
 * @property-read mixed|array $get              获取GET参数
 * @property-read mixed|array $post             获取POST参数
 * @property-read array $all                    GET+POST参数
 * @property-read array $filteredGet            过滤后的GET参数
 * @property-read array $filteredPost           过滤后的POST参数
 * @property-read array $filteredAll            过滤后的GET+POST参数
 * @property-read boolean $isGet                当前是否是GET请求
 * @property-read boolean $isPost               当前是否是POST请求
 * @property-read boolean $isAjax               当前是否是AJAX请求
 * @property-read string|null $domain           当前域名
 * @property-read \yii\web\Request $request     request对象
 * @property-read \yii\web\Response $response   response对象
 * @author cleverstone
 * @since ym2.0
 */
trait Http
{
    /**
     * 获取GET参数
     * @param null $name 参数名
     * @param null $default 默认值
     * @return array|mixed
     */
    public function getGet($name = null, $default = null)
    {
        return Yii::$app->request->get($name, $default);
    }

    /**
     * 获取POST参数
     * @param null $name 参数名
     * @param null $default 默认值
     * @return array|mixed
     */
    public function getPost($name = null, $default = null)
    {
        return Yii::$app->request->post($name, $default);
    }

    /**
     * 值是否是空
     * @param mixed $value 值
     * @return bool
     */
    public function isEmpty($value)
    {
        return $value === '' || $value === null || $value === [];
    }

    /**
     * 过滤掉值为空的数据
     * @param array $params 参数
     * @return array
     */
    public function filterEmpty(array $params)
    {
        $result = [];
        if (!empty($params)) {
            foreach ($params as $name => $value) {
                if (!$this->isEmpty($value)) {
                    $result[$name] = $value;
                }
            }
        }

        return $result;
    }

    /**
     * 获取过滤后的GET参数
     * @return array
     */
    public function getFilteredGet()
    {
        return $this->filterEmpty($this->request->get());
    }

    /**
     * 获取过滤后的POST参数
     * @return array
     */
    public function getFilteredPost()
    {
        return $this->filterEmpty($this->request->post());
    }

    /**
     * 获取过滤后的GET + POST参数
     * @param bool $mergeGet true:合并GET false:合并POST
     * @return array
     */
    public function getFilteredAll($mergeGet = true)
    {
        return $this->filterEmpty($this->getAll($mergeGet));
    }

    /**
     * 获取所有参数(GET + POST)
     * @param bool $mergeGet true:合并GET false:合并POST
     * @return array
     */
    public function getAll($mergeGet = true)
    {
        $get = $this->request->get();
        $post = $this->request->post();

        if (true === $mergeGet) {
            $all = array_merge($get, $post);
        } else {
            $all = array_merge($post, $get);
        }

        return $all;
    }

    /**
     * 获取当前域名
     * @return string|null
     */
    public function getDomain()
    {
        return $this->request->hostInfo;
    }

    /**
     * 是否是GET请求
     * @return bool
     */
    public function getIsGet()
    {
        return $this->request->isGet;
    }

    /**
     * 是否是POST请求
     * @return bool
     */
    public function getIsPost()
    {
        return $this->request->isPost;
    }

    /**
     * 是否是AJAX请求
     * @param boolean $general 是否泛指
     * @return bool
     */
    public function getIsAjax($general = true)
    {
        return $general ? (
            $this->request->getIsAjax()
            || HttpHelper::isAcceptJson()
            || key_exists(App::params('http.ajax.identity'), $this->getAll())
        ) : $this->request->getIsAjax();
    }

    /**
     * 获取request
     * @return \yii\web\Request
     */
    public function getRequest()
    {
        return Yii::$app->request;
    }

    /**
     * 获取response
     * @return \yii\web\Response
     */
    public function getResponse()
    {
        return Yii::$app->response;
    }

    /**
     * JSON响应(成功)
     * @param string $msg 消息
     * @param array $data 数据
     * @return Response
     */
    public function asOk($msg = 'Success', $data = [])
    {
        return $this->asJsonX(App::params('http.res.success'), $data, $msg);
    }

    /**
     * JSON响应(失败)
     * @param string $msg 消息
     * @return Response
     */
    public function asFail($msg = 'Error')
    {
        return $this->asJsonX(App::params('http.res.failure'), [], $msg);
    }

    /**
     * JSON响应(未授权)
     * @param string $msg 提示语
     * @return Response
     */
    public function as403($msg = '')
    {
        $msg = $msg ?: Yii::t('app.common', 'Your request forbidden');
        return $this->asJsonX(App::params('http.res.unauthorized'), [], $msg);
    }

    /**
     * JSON响应(扩展)
     * @param int $code 业务码
     * @param mixed $data 数据
     * @param string $msg 消息
     * @return Response
     */
    public function asJsonX($code, $data, $msg)
    {
        $this->response->format = Response::FORMAT_JSON;
        $this->response->data = compact('code', 'data', 'msg');

        return $this->response;
    }
}